package com.dhcc.DRGsHosp.application.impl;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.OperInfoApplication;
import com.dhcc.DRGsHosp.core.domain.KPIInfo;
import com.dhcc.DRGsHosp.core.domain.OperInfo;
import com.dhcc.DRGsHosp.infra.util.ConstantEnum;

/**
 * @title		:OperInfoApplicationImpl.java
 * @description	:
 * @author		:WangTeng
 * @date		:2016年5月20日
 * @version     :1.0
 * @copyright   :DHCC 2013-2016
 */
@Named
@Transactional
public class OperInfoApplicationImpl implements OperInfoApplication {
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository repository;
	
	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}

	@Override
	public void updateOperInfo(List<OperInfo> operInfos) {
		StringBuilder updateSql = null;
		BufferedWriter bout = null;
		try {
			bout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ConstantEnum.DATAPATH+"/updatSql.txt"),"utf-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (OperInfo operInfo : operInfos) {
			
			//手术信息
			updateSql = new StringBuilder();
			updateSql.append("UPDATE DRGS.T_OPERATION SET OPER_STIME = TO_DATE('");
			updateSql.append(operInfo.getOperStartDate()==null?"":operInfo.getOperStartDate());
			updateSql.append(operInfo.getOperStartTime()==null?"":" "+operInfo.getOperStartTime());
			updateSql.append("','YYYY-MM-DD HH24:MI'), OPER_ETIME = TO_DATE('");
			updateSql.append(operInfo.getOperEndDate()==null?"":operInfo.getOperEndDate());
			updateSql.append(operInfo.getOperEndTime()==null?"":" "+operInfo.getOperEndTime());
			updateSql.append("','YYYY-MM-DD HH24:MI'), ANESTHESIA_STIME = TO_DATE('");
			updateSql.append(operInfo.getAnesStartDate()==null?"":operInfo.getAnesStartDate());
			updateSql.append(operInfo.getAnesStartTime()==null?"":" "+operInfo.getAnesStartTime());
			updateSql.append("','YYYY-MM-DD HH24:MI'), ANESTHESIA_ETIME = TO_DATE('");
			updateSql.append(operInfo.getAnesEndDate()==null?"":operInfo.getAnesEndDate());
			updateSql.append(operInfo.getAnesEndTime()==null?"":" "+operInfo.getAnesEndTime());
			if (operInfo.getLeaveRoomTime()!=null) {
				updateSql.append("','YYYY-MM-DD HH24:MI'), LEAVE_TIME = TO_DATE('");
				updateSql.append(operInfo.getLeaveRoomTime());
			}
			updateSql.append("','YYYY-MM-DD HH24:MI'), OPER_ROOM_CODE = '");
			updateSql.append(operInfo.getOperRoomId());
			updateSql.append("', OPER_ROOM_DESC = '");
			updateSql.append(operInfo.getOperRoom());
			//updateSql.append("', DOCTOR_ARRIVE_TIME = ''");
			updateSql.append("'  WHERE ");
			updateSql.append("PARID = '");
			updateSql.append(operInfo.getParId());
			updateSql.append("' AND OPERATION = '");
			updateSql.append(operInfo.getOperDesc());
			updateSql.append("'");
			
		    try {
		    	bout.write(updateSql.toString());
				bout.newLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			getRepository().createSqlQuery(updateSql.toString()).executeUpdate();
			
			//院感信息
			String infUpdateSql = "UPDATE DRGS.T_MEDICALRECORD SET INFECTION_CASE='"+operInfo.getInfInfo()+"' WHERE ID='"+operInfo.getParId()+"'";
			getRepository().createSqlQuery(infUpdateSql.toString()).executeUpdate();
		}
		
		try {
			bout.flush();
			bout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void saveOrUpdate(List<KPIInfo> kpiInfos) {
		for (KPIInfo kpiInfo : kpiInfos) {
			String selectSql = "SELECT * FROM DRGS.T_MKPI WHERE DIM_ID='"+kpiInfo.getDepCode()+"' "
					+ "AND KPI_CODE = '"+kpiInfo.getKpiCode()+"' "
					+ "AND DATE_FROM = TO_DATE('"+kpiInfo.getDateFrom()+"','YYYY-MM-DD') "
					+ "AND DATE_TO = TO_DATE('"+kpiInfo.getDateTo()+"','YYYY-MM-DD')";
			
			List<Object[]> list = getQueryChannelService().createSqlQuery(selectSql).list();
			if (list.size()>0) { 
				String updateSql = "UPDATE DRGS.T_MKPI SET KPI_CODE='"+kpiInfo.getKpiCode()+"',"
						+ "KPI_DESC='"+kpiInfo.getKpiDesc()+"',VALUE='"+kpiInfo.getKpiValue()+"' "
						+ "WHERE DIM_ID='"+kpiInfo.getDepCode()+"' "
						+ "AND KPI_CODE = '"+kpiInfo.getKpiCode()+"' "
						+ "AND DATE_FROM = TO_DATE('"+kpiInfo.getDateFrom()+"','YYYY-MM-DD') "
						+ "AND DATE_TO = TO_DATE('"+kpiInfo.getDateTo()+"','YYYY-MM-DD')";
				getRepository().createSqlQuery(updateSql).executeUpdate();
			} else {
				String insertSql = "INSERT INTO DRGS.T_MKPI ( ID, KPI_CODE, KPI_DESC, DIM_ID, DIM_DESC, VALUE, DATE_FROM, DATE_TO ) "
						+ "VALUES ( '"+kpiInfo.getId()+"', '"+kpiInfo.getKpiCode()+"', "
						+ "'"+kpiInfo.getKpiDesc()+"', '"+kpiInfo.getDepCode()+"', "
						+ "'"+kpiInfo.getDepDesc()+"', '"+kpiInfo.getKpiValue()+"', "
						+ "TO_DATE ('"+kpiInfo.getDateFrom()+"', 'YYYY-MM-DD'), "
						+ "TO_DATE ('"+kpiInfo.getDateTo()+"', 'YYYY-MM-DD'))";
				getRepository().createSqlQuery(insertSql).executeUpdate();
			}
		}
	}

}
